package com.piggy.sys.controller;

import cn.hutool.core.util.BooleanUtil;
import com.piggy.common.core.constant.SecurityConstants;
import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.FeignResultUtils;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.annotation.Inner;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.api.RemoteLogiMenuService;
import com.piggy.logi.sys.api.bo.LogiSysMenuBo;
import com.piggy.sys.domain.SysMenu;
import com.piggy.sys.service.ISysMenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 *
 * @author shark
 */
@RestController
@RequestMapping("/logi/menu")
public class LogiSysMenuController extends BaseController {
    @Resource
    private RemoteLogiMenuService menuService;

    /**
     * 获取菜单列表
     */
    //@SaCheckPermission("system:menu:list")
    @GetMapping("/list")
    public R list(LogiSysMenuBo menu) {
        Long userId = SecurityUtils.getUserId();
        R menus = menuService.innerList(menu, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(menus);
        return menus;
    }


    /**
     * 根据菜单编号获取详细信息
     */
    //@SaCheckPermission("system:menu:query")
    @GetMapping(value = "/{menuId}")
    public R getInfo(@PathVariable Long menuId) {
        R  r = menuService.innerInfo(menuId, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public R treeselect(LogiSysMenuBo menu) {
        R menus = menuService.innerTreeselect(menu, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(menus);
        return menus;
    }

    /**
     * 新增菜单
     */
    //@SaCheckPermission("system:menu:add")
    //@Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody LogiSysMenuBo menu) {
        menu.setCreateBy(SecurityUtils.getUsername());
        R r = menuService.innerAdd(menu, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    /**
     * 修改菜单
     */
    //@SaCheckPermission("system:menu:edit")
    //@Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody LogiSysMenuBo menu) {
        menu.setUpdateBy(SecurityUtils.getUsername());
        R r = menuService.innerEdit(menu, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

    /**
     * 删除菜单
     */
    //@SaCheckPermission("system:menu:remove")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public R remove(@PathVariable("menuId") Long menuId) {
        R r = menuService.innerRemove(menuId, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(r);
        return r;
    }

}
